package com.cloudcc.mobile.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrFrameLayout;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.util.SystemUtils;

import static android.view.MotionEvent.ACTION_DOWN;
import static android.view.MotionEvent.ACTION_UP;

/**
 * Created by lin on 2017/7/11.
 */

public class CreatePopwindow extends PopupWindow {

    private Context mcontext;
    private View contentView1;
    private CreatPop lister;

    private ImageView imageView1, imageView2;
    private LinearLayout ctxldr_ll, ptxj_ll;
    private TextView textpop, ptxj;

    public CreatePopwindow(Context context) {

        mcontext = context;
        LayoutInflater inflater = (LayoutInflater) mcontext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        contentView1 = inflater.inflate(R.layout.create_pop, null);

        imageView1 = (ImageView) contentView1.findViewById(R.id.creat_pop_image1);
        imageView2 = (ImageView) contentView1.findViewById(R.id.creat_pop_image2);
        ctxldr_ll = (LinearLayout) contentView1.findViewById(R.id.ctxldr_ll);
        textpop = (TextView) contentView1.findViewById(R.id.textpop);
        ptxj = (TextView) contentView1.findViewById(R.id.ptxj);
        ptxj_ll = (LinearLayout) contentView1.findViewById(R.id.ptxj_ll);

        ctxldr_ll.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == ACTION_DOWN) {
                    textpop.setTextColor(mcontext.getResources().getColor(R.color.presss_colot_t));
                } else if (event.getAction() == ACTION_UP) {
                    textpop.setTextColor(mcontext.getResources().getColor(R.color.lxrpop));
                }
                return false;
            }
        });
        ctxldr_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dismiss();
                lister.creatOne();
            }

        });
        ptxj_ll.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == ACTION_DOWN) {
                    ptxj.setTextColor(mcontext.getResources().getColor(R.color.presss_colot_t));
                } else if (event.getAction() == ACTION_UP) {
                    ptxj.setTextColor(mcontext.getResources().getColor(R.color.lxrpop));
                }
                return false;
            }
        });
        ptxj_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                lister.creatTow();

            }
        });

        // 设置SelectPicPopupWindow的View
        this.setContentView(contentView1);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(PtrFrameLayout.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(PtrFrameLayout.LayoutParams.WRAP_CONTENT);
        // 获取屏幕信息
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 刷新状态
        this.update();
        // 实例化一个ColorDrawable颜色为半透明
//        ColorDrawable dw = new ColorDrawable(Color.TRANSPARENT);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                // popupWindow隐藏时恢复屏幕正常透明度
                setBackgroundAlpha(1.0f);
                lister.ondismiss();
            }
        });
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha 屏幕透明度0.0-1.0 1表示完全不透明
     */
    public void setBackgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = ((Activity) mcontext).getWindow()
                .getAttributes();
        lp.alpha = bgAlpha;
        ((Activity) mcontext).getWindow().setAttributes(lp);
    }

    /**
     * 显示popwindow
     * @param parent 显示的位置父view
     * @param with X轴距离
     * @param height Y轴距离
     */
    public void showPopupWindow(View parent, int with, int height) {
        if (!this.isShowing()) {
            setBackgroundAlpha(0.5f);//设置屏幕透明度
            this.showAtLocation(parent, Gravity.TOP, with, (int) (height - (contentView1.getHeight() == 0 ? SystemUtils.dpToPx(110, mcontext) : contentView1.getHeight())));
        } else {
            this.dismiss();
        }
    }

    /**
     * 设置显示的图片和文字
     * @param image1 图标1
     * @param image2 图标2
     * @param text1 文案1
     * @param text2 文案2
     */
    public void setImageAndText(int image1 , int image2 , String text1 , String text2){
        imageView1.setImageResource(image1);
        imageView2.setImageResource(image2);
        textpop.setText(text1);
        ptxj.setText(text2);
    }

    //设置接口实现类
    public void setOnCreatPopListener(CreatPop listener) {
        this.lister = listener;
    }

    //定义回调接口，第一行点击事件、第二行点击事件、dismiss回调事件
    public interface CreatPop {
        void creatOne();

        void creatTow();

        void ondismiss();
    }
}
